package com.uenpay.im.invokeitems.getunreadmsg;

import android.text.TextUtils;

import com.qiyunxin.android.http.net.HttpInvokeItem;
import com.qyx.android.message.PushServiceShareData;
import com.uenpay.im.configuration.APIConfiguration;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * 获取类型消息
 * 
 * @author SL
 * @copyRight http://www.qiyunxin.com
 */
public class GetJsonUnreadMsg extends HttpInvokeItem {

	public class GetUnReadMsgResult {
		public int status;
		public ArrayList<JSONObject> messageJsons = new ArrayList<JSONObject>();
		public String msg;
		public String ids = "";
	}

	public GetUnReadMsgResult getOutput() {
		return (GetUnReadMsgResult) GetResultObject();
	}

	public GetJsonUnreadMsg() {
		String url = APIConfiguration.getBaseUrl()
				+ "Chat/Chat/getUnreadMsgList?pageSize=100&_custid="
				+ PushServiceShareData.getInstance().getCustId() + "&_token="
				+ PushServiceShareData.getInstance().getToken();
		SetUrl(url);
	}

	@Override
	protected Object DeserializeResult(String responseBody) throws Exception {
		GetUnReadMsgResult result = new GetUnReadMsgResult();

		try {
			JSONObject json = new JSONObject(responseBody);
			result.status = json.optInt("status");
			result.msg = json.optString("msg");
			JSONArray jsondata = json.optJSONArray("data");
			if (jsondata != null) {
				for (int i = 0; i < jsondata.length(); i++) {
					JSONObject idsJson = jsondata.optJSONObject(i);
					if (TextUtils.isEmpty(result.ids)) {
						result.ids = idsJson.optString("msg_id");
					} else {
						result.ids = result.ids + ","
								+ idsJson.optString("msg_id");
					}
					result.messageJsons.add(idsJson);
				}
			}

		} catch (Exception e) {
			return result;
		}
		return result;
	}

}
